#!/usr/bin/env python
import os
import sys
import Scanners

__author__='Chase Schultz'
__version__ = '0.1'

def help():
    ''' Displays a helpful message to stdout '''
    printAscii()
    print '\nUsage:\n\tpynap.py -t [target] [options]'
    print 'Required:'
    print '\t-t.........................Target(may be a single or range)'
    print 'Options:'
    print '\t-p [number]................Port numbers to scan e.g(1-1337)'
    print '\t-o.........................Create an output file at [path]'
    print '\t--help.....................Display this message\n'

def executeChild():
    '''Set and Execute tool'''
    setTarget()
    setPort()
    setOutput()
    scan.retriveScantypes()
    scanTypelist = scan.scanTypes.keys()
    scanTypelist.sort()
    scanSelection = int(displayScantypes(scanTypelist))
    setScanflags(scanSelection, scanTypelist)
    scan.runScan()
    if scan.fileSaveboolean:
        scan.saveTofile()
    else:
        scan.displayResults()
        
def executeChildren():
    '''Run all tools with all options'''
    setTarget()
    setPort()
    setOutput()
    scan.retriveScantypes()
    scanTypelist = scan.scanTypes.keys()
    scanTypelist.sort()
    for index in range(0,len(scanTypelist)):
        setScanflags(index, scanTypelist)
        scan.runScan()
        if scan.fileSaveboolean:
            scan.saveTofile()
        else:
            scan.displayResults()  

    
def displayScantypes(scanTypelist):
    '''Display the Scan Types'''
    print 'Scan Types:'
    i = 1
    for flags in scanTypelist:
        print '%i ) %s' % (i, flags)
        i += 1
    
    scanSelection = int(raw_input('What type of scan would you like to do:'))
    scanSelection -= 1
    return scanSelection

def setTarget(): 
    ''' Collects arguments from argv to hand to child process '''
   
    if '-t' in sys.argv:
        scan.target = (sys.argv[sys.argv.index('-t') + 1])
    else:
        print 'Target is not specified!'
        sys.exit()

def setPort():
    '''Sets the port variable if specified'''    
    if '-p' in sys.argv:
        scan.port = (sys.argv[sys.argv.index('-p') + 1])

def setScanflags(index, scanFlagslist):
    '''Sets the scan flags according to choices'''
    flagKey = scanFlagslist[index]
    scan.scanFlags = scan.scanTypes[flagKey]

def setOutput():
    '''If output file is specified'''
    if '-o' in sys.argv:
        scan.fileSaveboolean = True
        scan.saveFile = (sys.argv[sys.argv.index('-o') + 1]) 
    
def getTool():
    '''Get tool to use from user'''
    print 'What tools would you like to use?\n'
    print 'Supported Tools:'
    print '1) nmap'
    print '2) xprobe'
    print '3) netcat'
    print '4) RAM THE GATE(all)'
    print ''
    tool = raw_input('Tool Selection? (Select Number):')
    if tool == '1':
        return 'nmap'
    elif tool == '2':
        return 'xprobe2'
    elif tool == '3':
        return 'netcat'
    elif tool == '4':
        return 'alltools'
    else:
        return 'alltools'


def printAscii():
    ''' Prints an ascii banner '''
    print '    ____  __  ______  ____ _____'
    print '   / __ \/ / / / __ \/ __ `/ __ \\'
    print '  / /_/ / /_/ / / / / /_/ / /_/ /'
    print ' / .___/\__, /_/ /_/\__,_/ .___/' 
    print '/_/    /____/           /_/'      
    print ''
    print 'Python Nmap Automator: pynap.py - v%s' % __version__
    print 'Author: %s' % __author__
    print '\n---------------------------------------\n'


#Main of Pynap
if __name__ == '__main__':
    '''Main scope of Pynap'''
    if '--help' in sys.argv or '-h' in sys.argv or len(sys.argv) == 1:
        help()
        os._exit(0)
    try:
        printAscii()
        tool = getTool()
        if tool == 'nmap':
            scan = Scanners.Nmap()
            executeChild()
        elif tool == 'xprobe2':
            scan = Scanners.Xprobe()
            executeChild()
        elif tool == 'netcat':
            scan = Scanners.Netcat()
            executeChild()
        elif tool == 'alltools':
            scan = Scanners.Nmap()
            executeChildren()
            scan = Scanners.Xprobe()
            executeChildren()
            scan = Scanners.Xprobe()
            executeChildren()    
          
        
    except KeyboardInterrupt:
        print '\n[!] User interrupt, exiting'
    os._exit(0)
